﻿using Oracle.ManagedDataAccess.Client;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http;

namespace Waiyun.WebApi
{
    /// <summary>
    /// 空运数据
    /// </summary>
    public class AirSDController : ApiController
    {
        public static string ConnectionString;

        static AirSDController()
        {
            ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["waiyun_v2"].ConnectionString;
        }

        /// <summary>
        /// 接受委托
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public SingleResult<long> Delegate()
        {
            string name = "airsd\\delegate";
            if (ThreadPools.Get(name) == null)
            {
                Thread thread = new Thread(() =>
                {
                    string errorMessage = null;
                    OracleConnection connection = ConnectionPool.GetConnection(ConnectionString, out errorMessage);
                    if (connection == null)
                    {
                        return;
                    }
                    try
                    {
                        long value = 0;
                        using (OracleCommand command = new OracleCommand(@"select count(1) from od_f_stats_air_exp o where o.n_poacp_num > 0 and o.d_poacp_date >= trunc(sysdate)", connection))
                        {
                            using (OracleDataReader reader = command.ExecuteReader())
                            {
                                if (reader.Read())
                                {
                                    value = Convert.ToInt64(reader[0]);
                                }
                                reader.Close();
                                Cache2.Set(name, value);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        return;
                    }
                    finally
                    {
                        ConnectionPool.Release(connection);
                    }
                    ThreadPools.Stop(name);
                });
                thread.Name = name;
                ThreadPools.Start(thread);
            }
            object cacheValue = Cache2.Get(name);
            if (cacheValue != null)
            {
                return new SingleResult<long>(Convert.ToInt64(cacheValue));
            }
            return new SingleResult<long>(0);
        }

        /// <summary>
        /// 订舱交运
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public SingleResult<long> Booking()
        {
            string name = "airsd\\booking";
            if (ThreadPools.Get(name) == null)
            {
                Thread thread = new Thread(() =>
                {
                    string errorMessage = null;
                    OracleConnection connection = ConnectionPool.GetConnection(ConnectionString, out errorMessage);
                    if (connection == null)
                    {
                        return;
                    }
                    try
                    {
                        long value = 0;
                        using (OracleCommand command = new OracleCommand(@"select count(1) from od_f_stats_air_exp o where o.N_AEGTR_NUM > 0 and o.D_AEGTR_DATE >= trunc(sysdate)", connection))
                        {
                            using (OracleDataReader reader = command.ExecuteReader())
                            {
                                if (reader.Read())
                                {
                                    value = Convert.ToInt64(reader[0]);
                                }
                                reader.Close();
                                Cache2.Set(name, value);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        return;
                    }
                    finally
                    {
                        ConnectionPool.Release(connection);
                    }
                    ThreadPools.Stop(name);
                });
                thread.Name = name;
                ThreadPools.Start(thread);
            }
            object cacheValue = Cache2.Get(name);
            if (cacheValue != null)
            {
                return new SingleResult<long>(Convert.ToInt64(cacheValue));
            }
            return new SingleResult<long>(0);
        }

        /// <summary>
        /// 出港
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public SingleResult<long> Leave()
        {
            string name = "airsd\\leave";
            if (ThreadPools.Get(name) == null)
            {
                Thread thread = new Thread(() =>
                {
                    string errorMessage = null;
                    OracleConnection connection = ConnectionPool.GetConnection(ConnectionString, out errorMessage);
                    if (connection == null)
                    {
                        return;
                    }
                    try
                    {
                        long value = 0;
                        using (OracleCommand command = new OracleCommand(@"select count(1) from od_f_stats_air_exp o where o.N_AEOTD_NUM > 0 and o.D_AEOTD_DATE >= trunc(sysdate)", connection))
                        {
                            using (OracleDataReader reader = command.ExecuteReader())
                            {
                                if (reader.Read())
                                {
                                    value = Convert.ToInt64(reader[0]);
                                }
                                reader.Close();
                                Cache2.Set(name, value);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        return;
                    }
                    finally
                    {
                        ConnectionPool.Release(connection);
                    }
                    ThreadPools.Stop(name);
                });
                thread.Name = name;
                ThreadPools.Start(thread);
            }
            object cacheValue = Cache2.Get(name);
            if (cacheValue != null)
            {
                return new SingleResult<long>(Convert.ToInt64(cacheValue));
            }
            return new SingleResult<long>(0);
        }

        /// <summary>
        /// 到达
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public SingleResult<long> Arrived()
        {
            string name = "airsd\\arrived";
            if (ThreadPools.Get(name) == null)
            {
                Thread thread = new Thread(() =>
                {
                    string errorMessage = null;
                    OracleConnection connection = ConnectionPool.GetConnection(ConnectionString, out errorMessage);
                    if (connection == null)
                    {
                        return;
                    }
                    try
                    {
                        long value = 0;
                        using (OracleCommand command = new OracleCommand(@"select count(1) from od_f_stats_air_exp o where o.N_AEAAD_NUM > 0 and o.D_AEAAD_DATE >= trunc(sysdate)", connection))
                        {
                            using (OracleDataReader reader = command.ExecuteReader())
                            {
                                if (reader.Read())
                                {
                                    value = Convert.ToInt64(reader[0]);
                                }
                                reader.Close();
                                Cache2.Set(name, value);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        return;
                    }
                    finally
                    {
                        ConnectionPool.Release(connection);
                    }
                    ThreadPools.Stop(name);
                });
                thread.Name = name;
                ThreadPools.Start(thread);
            }
            object cacheValue = Cache2.Get(name);
            if (cacheValue != null)
            {
                return new SingleResult<long>(Convert.ToInt64(cacheValue));
            }
            return new SingleResult<long>(0);
        }
    }
}
